package com.woniuxy.credit_loans.product_liu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.entity.Product;
import com.woniuxy.credit_loans.entity.ProductLabel;
import com.woniuxy.credit_loans.product_liu.dto.PageLJData;
import com.woniuxy.credit_loans.product_liu.dto.ProductDto;
import com.woniuxy.credit_loans.product_liu.service.ProductLabelService;
import com.woniuxy.credit_loans.product_liu.service.ProductService;
import com.woniuxy.credit_loans.product_liu.vo.EchartsVo;
import com.woniuxy.credit_loans.product_liu.vo.ProductVo;
import com.woniuxy.credit_loans.util.ObjectExcelView;
import com.woniuxy.credit_loans.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "产品表中的相关接口")
@RestController
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductLabelService productLabelService;

    //查询所有产品信息，2022/12/6 14:30  --刘杰
    @ApiOperation(value = "主页中查询所有的产品信息")
    @PostMapping("")
    public Result selectAllProduct(@RequestBody ProductDto productDto){
        PageHelper.startPage(productDto.getCurrentPage(),productDto.getPageSize());
        List<ProductVo> products = productService.selectAllProduct();
        PageInfo<ProductVo> pageInfo = new PageInfo<>(products);
        return Result.ok().data("products",products).data("total",pageInfo.getTotal());
    }

    //查询所有个人产品信息，2022/12/7 11:20  --刘杰
    @ApiOperation(value = "员工产品页面，查询所有个人类产品信息")
    @PostMapping("/personProduct")
    public Result selectPersonProduct(@RequestBody ProductDto productDto){
        PageHelper.startPage(productDto.getCurrentPage(),productDto.getPageSize());
        List<ProductVo> products = productService.selectPersonProduct();
        PageInfo<ProductVo> pageInfo = new PageInfo<>(products);
        return Result.ok().data("products",products).data("total",pageInfo.getTotal());
    }

    //查询所有产品信息，2022/12/7 11:20  --刘杰
    @ApiOperation(value = "员工产品页面，查询所有公司类产品信息")
    @PostMapping("/companyProduct")
    public Result selectCompanyProduct(@RequestBody ProductDto productDto){
        PageHelper.startPage(productDto.getCurrentPage(),productDto.getPageSize());
        List<ProductVo> products = productService.selectCompanyProduct();
        PageInfo<ProductVo> pageInfo = new PageInfo<>(products);
        return Result.ok().data("products",products).data("total",pageInfo.getTotal());
    }

    //根据产品名称模糊查询，2022/12/6 15:12 --刘杰
    @ApiOperation(value = "主页中，根据产品名称或类型模糊查询")
    @PostMapping("/nameLike")
    public Result selectByProductName(@RequestBody Product product){
        List<ProductVo> products = productService.selectByLike(product);
        return Result.ok().data("productList",products);
    }

    @ApiOperation(value = "产品页面中查询产品标签")
    @GetMapping("/productLabels")
    public Result selectAllLabels(){
        List<ProductLabel> productLabels = productLabelService.selectAllLabels();
        return Result.ok().data("productLabel",productLabels);
    }

    @ApiOperation(value = "主页中，点击按钮显示产品类别占比图")
    @GetMapping("/countByType")
    public Result classifyByType(){
        List<EchartsVo> echartsVos = productService.classifyByType();
        return Result.ok().data("classifyByType",echartsVos);
    }

    @ApiOperation(value = "员工产品中根据产品id查询产品信息")
    @PostMapping("{id}")
    public Result selectOneProduct(@PathVariable Long id){
        Product product = productService.selectOne(id);
        return Result.ok().data("updateForm",product);
    }

    @ApiOperation(value = "员工产品中，修改某个产品信息，固定修改商品状态为待审核")
    @PutMapping("/updateProduct")
    public Result updateProductInfo(@RequestBody Product product){
        product.setProductFlag(1);
        int i = productService.updateByPrimaryKeySelective(product);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "员工产品中，点击下架，修改产品状态为下架中")
    @GetMapping("{id}")
    public Result updateProductFlag(@PathVariable Long id){
        Product product = productService.selectOne(id);
        product.setProductFlag(4);
        int i = productService.updateByPrimaryKeySelective(product);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "用户产品页面，导出产品信息")
    @PostMapping("/exportExcel")
    public ModelAndView exportExcel(@RequestBody ProductDto productDto){
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("产品编号");
            titles.add("产品名称");
            titles.add("产品描述");
            titles.add("贷款额度");
            titles.add("产品类型");
            titles.add("还款方式");
            titles.add("产品状态");
            titles.add("产品标签");
            dataMap.put("titles",titles);
            List<ProductVo> productVos = productService.selectByExport(productDto);
            List<PageLJData> varList = new ArrayList<>();
            for (ProductVo vo : productVos) {
                PageLJData pd = new PageLJData();
                pd.put("var1",vo.getProductId());
                pd.put("var2",vo.getProductName());
                pd.put("var3",vo.getProductDesc());
                pd.put("var4",vo.getProductAmount());
                pd.put("var5",vo.getProductTypeName());
                pd.put("var6",vo.getAverageName());
                pd.put("var7",vo.getProductFlag());
                pd.put("var8",vo.getProductLabelFlag());
                varList.add(pd);
            }
            dataMap.put("varList",varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
